package com.wisezone.demo0221;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) {
        //1.已经知道你要实例化类的名称
        Class<Student> clzz = Student.class;
        //2.已经知道类的实例对象
        Student student = new Student();
        Class<?> c1=student.getClass();
        //3.Class类的静态方法:报的字符串名称+类的字符串名称
        try {
            Class<?> clazz = Class.forName("com.wisezone.demo0221.Student");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //4.通过类的加载器：包的字符串
        try {
            Class<?> clz=Test.class.getClassLoader().loadClass("com.wisezone.demo0221.Student");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        //成员
        //属性
        Field field;
        //方法
        Method method;
        //构造器
        Constructor<?> constructor;
        Annotation annotation;



    }

    public Object getobject(String className){
        Object obj = null;
        if (className!=null&className.length()>0)
        {
            try {
                Class<?> clazz = Class.forName(className);
                obj = clazz.newInstance();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }

    public void setValue(Student student,String attrName,Object value){
        if (student!=null&& attrName!=null&&attrName.length()>0){
            try {
                Class<?> c1 = student.getClass();
                Field field = c1.getDeclaredField(attrName);
                //针对私有属性授权
                field.setAccessible(true);
                field.set(student,value);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}
